<?php

namespace App\Http\Controllers\API\Friend;

use Auth;
use Validator;
use App\Models\API\Friend;
use Illuminate\Http\Request;
use App\Http\Controllers\API\APIController;

class FriendController extends APIController
{
    public function index()
    {
        $userId = Auth::user()->id;

        $friends = Friend::where('user_id', $userId)
        ->join('users', 'friends.companion_id', '=', 'users.id')
        ->select('friends.*', 'users.user_name', 'users.user_img')
        ->paginate();
        return response()->json($friends, 200);
    }

    public function chatroom($userId, $companionId)
    {
        $chatroomId = Friend::where([
            ['user_id', '=', $userId],
            ['companion_id', '=', $companionId]
        ])->get();
        return response()->json($this->setJson(true, $chatroomId), 200);
    }

    public function destroy(Request $request, $id)
    {
    	// 今后升级
    }

    private function setJson($status, $msg)
    {
        $res = [
            'status' => $status,
            'msg'    => $msg
        ];
        return $res;
    }
}
